		TITLE	INSTSEG - Copyright (C) 1994 SLR Systems

		INCLUDE	MACROS
		INCLUDE	SEGMENTS
		INCLUDE	CLASSES

		PUBLIC	INSTALL_SEGMENT,ALLOC_SEGMENT,INSTALL_SEGMENT_SYMBOL


		.DATA

		EXTERNDEF	SEG_COMBINE:BYTE,CLASS_TYPE:BYTE,CLASS_PLTYPE:BYTE,SEG_TYPE_MASK_DEFAULT:BYTE,SYMBOL_TEXT:BYTE

		EXTERNDEF	SEG_NAME_LINDEX:DWORD,DATA_DEFAULTS:DWORD,CODE_DEFAULTS:DWORD,SEGMENT_HASH:DWORD,SYMBOL_HASH:DWORD
		EXTERNDEF	SYMBOL_LENGTH:DWORD,CLASS_SECTION_GINDEX:DWORD,_TEXT_SEGMENT_GINDEX:DWORD,CLASS_NAME_LINDEX:DWORD
		EXTERNDEF	SEG_CLASS_GINDEX:DWORD,CODE_CLASS_GINDEX:DWORD,SEGMENT_HASH_TABLE_PTR:DWORD,SEG_FRAME:DWORD
		EXTERNDEF	FIRST_CV_SEGMENT_GINDEX:DWORD,LAST_CV_SEGMENT_GINDEX:DWORD,CV_SEGMENT_COUNT:DWORD

		EXTERNDEF	CLASS_GARRAY:STD_PTR_S,LNAME_LARRAY:LARRAY_STRUCT,SEGMENT_GARRAY:STD_PTR_S,_TEXT_TPTR:TPTR_STRUCT
		EXTERNDEF	SYMBOL_TPTR:TPTR_STRUCT

		EXTERNDEF	CASE_STRING_COMPARE:DWORD


		.CODE	PASS1_TEXT

INSTALL_SEGMENT PROC
		;
		;SEG_CLASS IS VPTR TO CLASS
		;SEG_NAME_VPTR IS VPTR TO SEGMENT NAME
		;
		PUSHM	ESI,EDI

		MOV	EAX,SEG_NAME_LINDEX
		CONVERT_LINDEX_EAX_EAX	LNAME_LARRAY,ESI
		ASSUME	EAX:PTR TPTR_STRUCT

		PUSH	EBX
		MOV	EDI,OFF SYMBOL_TPTR
		ASSUME	EDI:PTR TPTR_STRUCT

		MOV	EBX,[EAX]._TP_FLAGS
		MOV	EDX,[EAX]._TP_HASH

		LEA	ESI,[EAX]._TP_TEXT
		MOV	ECX,[EAX]._TP_LENGTH

		ADD	EDX,EBX
		MOV	[EDI]._TP_LENGTH,ECX

		SHR	ECX,2
		MOV	[EDI]._TP_HASH,EDX

		INC	ECX
		LEA	EDI,[EDI]._TP_TEXT

		OPTI_MOVSD

		POPM	EBX,EDI

		POP	ESI

INSTALL_SEGMENT_SYMBOL	LABEL	PROC
		;
		;SYMBOL_TEXT CLASS NAME, EDX IS HASH, RETURNS EAX GINDEX, ECX PHYS
		;
		PUSHM	EDI

		MOV	EAX,SEG_CLASS_GINDEX
		MOV	EDI,EDX

		MOV	ECX,EAX
;		ROL	EAX,16
		XOR	EDX,EDX

		XOR	EAX,EDI
		PUSH	EBX

		MOV	EBX,SEGMENT_HASH_TABLE_PTR

		HASHDIV	SEGMENT_HASH

		PUSH	ESI

		MOV	EAX,DPTR [EBX+EDX*4]
		LEA	EBX,[EBX+EDX*4 - SEGMENT_STRUCT._SEG_NEXT_HASH_GINDEX]
SEGMENT_NEXT:
		TEST	EAX,EAX
		JZ	DO_SEGMENT_INSTALL

		MOV	EDX,EAX
		CONVERT	EAX,EAX,SEGMENT_GARRAY
		ASSUME	EAX:PTR SEGMENT_STRUCT
		MOV	EBX,EAX
		ASSUME	EBX:PTR SEGMENT_STRUCT

		MOV	ESI,[EAX]._SEG_HASH
		MOV	EAX,[EAX]._SEG_NEXT_HASH_GINDEX

		CMP	ESI,EDI
		JNZ	SEGMENT_NEXT

		CMP	[EBX]._SEG_CLASS_GINDEX,ECX
		JNZ	SEGMENT_NEXT
		;
		;OK, TEST TEXT FOR MATCH
		;
		PUSHM	ECX,EDI

		MOV	EDI,OFF SYMBOL_TEXT
		MOV	ECX,SYMBOL_LENGTH

		LEA	ESI,[EBX]._SEG_TEXT

		CALL	CASE_STRING_COMPARE

		POPM	EDI,ECX

		JNZ	SEGMENT_NEXT

		POP	ESI
		MOV	ECX,EBX

		POPM	EBX,EDI

		MOV	EAX,EDX

		STC				;CARRY MEANS INSTALL FAILED, ALREADY THERE

		RET

DO_SEGMENT_INSTALL:
		PUSH	EDI
		MOV	ECX,EBX

		JMP	AS_1

INSTALL_SEGMENT ENDP


if	fg_cv

DO_FIRST_CV:
		MOV	FIRST_CV_SEGMENT_GINDEX,EAX
		JMP	DFC_RET

endif

ALLOC_SEGMENT	PROC
		;
		;ECX IS PLACE TO STORE POINTER
		;EAX IS HASH
		;
		PUSHM	EDI,EBX,ESI
		PUSH	EAX
AS_1::
		MOV	EAX,SYMBOL_LENGTH
		MOV	ESI,ECX
		ASSUME	ESI:PTR SEGMENT_STRUCT

		ADD	EAX,SIZE SEGMENT_STRUCT-3
		SEGMENT_POOL_ALLOC		;ES:DI IS PHYS, AX LOG

		MOV	EBX,EAX
		MOV	CL,SEG_COMBINE

		INSTALL_POINTER_GINDEX	SEGMENT_GARRAY

if	fg_cv

		MOV	EDX,LAST_CV_SEGMENT_GINDEX
		MOV	EDI,CV_SEGMENT_COUNT

		TEST	EDX,EDX
		JZ	DO_FIRST_CV

		CONVERT	EDX,EDX,SEGMENT_GARRAY
		ASSUME	EDX:PTR SEGMENT_STRUCT

		MOV	[EDX]._SEG_NEXT_CV_GINDEX,EAX
DFC_RET::
		INC	EDI

		MOV	LAST_CV_SEGMENT_GINDEX,EAX
		MOV	CV_SEGMENT_COUNT,EDI
		
endif

		CMP	CL,SC_PRIVATE		;IF PRIVATE, IGNORE HASH TABLE AND _TEXT-NESS
		JZ	L1$

		MOV	ECX,SYMBOL_HASH
		MOV	EDX,_TEXT_TPTR._TP_HASH

		MOV	[ESI]._SEG_NEXT_HASH_GINDEX,EAX
		CMP	ECX,EDX

		JZ	L3$
L1$:
		MOV	EDX,EAX			;GINDEX
		MOV	EDI,EBX

		MOV	ECX,SEGMENT_STRUCT._SEG_TEXT/4
		XOR	EAX,EAX

		REP	STOSD

		POP	ECX			;HASH
		MOV	AL,SEG_COMBINE

		MOV	[EBX]._SEG_HASH,ECX
		MOV	CL,CLASS_TYPE

		CMP	AL,SC_ABSOLUTE
		PUSH	EDX

		MOV	AH,SEG_TYPE_MASK_DEFAULT;MASK ASEG OR MASK RELOC...
		JNZ	L4$

		MOV	AH,MASK SEG_ASEG
L4$:
		MOV	EDX,CV_SEGMENT_COUNT
		OR	AH,CL			;FLAGS FROM CLASS
		;
		;IF PRIVATE, CALL IT UNDEFINED FOR NOW...
		;
		CMP	AL,SC_PRIVATE
		JNZ	L45$

		MOV	AL,SC_UNDEFINED
L45$:
		MOV	[EBX]._SEG_CV_NUMBER,EDX

		MOV	[EBX]._SEG_COMBINE,AL	;_SEG_COMBINE & _SEG_TYPE
		MOV	ECX,SEG_CLASS_GINDEX

		MOV	[EBX]._SEG_TYPE,AH
		MOV	[EBX]._SEG_CLASS_GINDEX,ECX

if	fg_plink
		MOV	ECX,CLASS_SECTION_GINDEX
		MOV	AL,CLASS_PLTYPE
		MOV	[EBX]._SEG_SECTION_GINDEX,ECX
		MOV	[EBX]._SEG_PLTYPE,AL
endif
		MOV	ECX,100H 		;SEG_ORDER_FLAG&SEG_MAX_ALIGN
		MOV	EDX,DATA_DEFAULTS

		TEST	AH,MASK SEG_CLASS_IS_CODE
		JZ	L5$

		MOV	EDX,CODE_DEFAULTS
L5$:
		MOV	ESI,OFF SYMBOL_TEXT
		ASSUME	ESI:NOTHING
		MOV	[EBX]._SEG_ORDER_FLAG,CL

		AND	EDX,NOT MASK SR_MULTIPLE ;MASK OUT THESE BITS
		MOV	[EBX]._SEG_MAX_ALIGN,CH

		MOV	ECX,DPTR [ESI-4]
		MOV	[EBX]._SEG_OS2_FLAGS,EDX

		SHR	ECX,2
		MOV	AL,[EBX]._SEG_COMBINE

		INC	ECX

		REP	MOVSD

		CMP	AL,SC_ABSOLUTE
		JZ	L6$

		DEC	ECX
		POP	EAX

		MOV	[EBX]._SEG_OFFSET,ECX	;SET BASE TO -1 FOR OVERLAYS DETERMINING LOWEST BASE
		MOV	ECX,EBX

		POPM	ESI,EBX,EDI
		OR	ECX,ECX			;CLEAR CARRY, INSTALL SUCCESSFUL

		RET

L3$:
		MOV	ESI,CODE_CLASS_GINDEX
		MOV	ECX,SEG_CLASS_GINDEX

		CMP	ECX,ESI
		JNZ	L1$

		MOV	EDX,SYMBOL_LENGTH
		MOV	ECX,DPTR SYMBOL_TEXT

		CMP	EDX,5
		JNZ	L1$

		CMP	ECX,'XET_'
		JNZ	L1$

		MOV	_TEXT_SEGMENT_GINDEX,EAX
		JMP	L1$

L6$:
		;
		;FOR ASEG WE MUST DEFINE FRAME, OFFSET, AND OTHER NONSENSE
		;
		MOV	ECX,SEG_FRAME
		POP	EAX

		MOV	[EBX]._SEG_OFFSET,ECX
		MOV	[EBX]._SEG_FRAME,ECX

		MOV	ECX,EBX
		POPM	ESI,EBX,EDI

		OR	EAX,EAX

		RET

ALLOC_SEGMENT	ENDP


		END

